<?php defined('BASEPATH') or die("No access");
    class Product_model extends MY_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        //get all product
        public function getAll($limit,$offset,$status)
        {
            $this->db->order_by('product.product_order','asc');
            $this->db->limit($limit,$offset);
            if($status==0)
            {
                $this->db->where('product.product_publish',0);
            }
            else
            {
                if($status>0)
                {
                   $this->db->where('product.product_publish',1); 
                }
            }
            $this->db->from('product');
            $this->db->join('category_product','category_product.category_product_id=product.product_category');
            $query=$this->db->get();
            return $query->result(); 
        }
        
        //get product by type deal
        public function getProductByTypeDeal($type,$limit,$offset)
        {
            $this->db->where('product.product_publish',1);
            $this->db->order_by('product.product_order','asc');
            if($type==0) // get by today
            {
                $this->db->where('product_begin_day <=',mdate('%Y%m%d',now()));
                $this->db->where('product_end_day >=',mdate('%Y%m%d',now()));
            }
            else
            {
                $this->db->where('product_end_day <',mdate('%Y%m%d',now()));
            }
            $this->db->from('product');
            $this->db->join('category_product','category_product.category_product_id=product.product_category');
            $this->db->limit($limit,$offset);
            $query=$this->db->get();
            return $query->result();
        }
        
        //get products by Category
        public function getProductByCategory($category)
        {
            $this->db->where('product.product_category',$category);
            $this->db->where('product.product_publish',1);
            $this->db->order_by('product.product_order','asc');
            $this->db->from('product');
            $this->db->join('category_product','category_product.category_product_id=product.product_category');
            $query=$this->db->get();
            return $query->result();
        }
        //get product by id 
        public function getProductById($id)
        {
            $this->db->where('product_id',$id);
            $query=$this->db->get('product');
            return $query->row();
        }
        
        //get product by alias 
        public function getProductByAlias($alias)
        {
            $this->db->where('product.product_alias',$alias);
            $query=$this->db->get('product');
            return $query->row();
        }
        
        //get product by alias 
        public function getProductByName($name)
        {
            //$this->db->select('product_alias,product_name,product_image_path');
            $this->db->like('product.product_name',$name);
            $this->db->where('product.product_publish',1);
            $this->db->order_by('product.product_order','asc');
            $this->db->from('product');
            $this->db->join('category_product','category_product.category_product_id=product.product_category');
            //$this->db->limit($limit,$offset);
            $query=$this->db->get();
            return $query->result();
        }
        // insert or update 
        public function save($id,$data=array())
        {
            if($id==0)
            {
                //insert new record
                $this->db->insert('product',$data);
            }
            else
            {
                $this->db->where('product_id',$id);
                $this->db->update('product',$data);
            }
            return $this->db->affected_rows();
        }
        //save order
        public function saveorder($id=array(),$order=array())
        {
            for($i=0;$i<count($id);$i++)
            {
                $this->db->where('product_id',$id[$i]);
                $this->db->update('product',array('product_order'=>$order[$i]));
            }
        }
        public function countBuyerBuyID($id)
        {
            $this->db->select('cart_product');
            $this->db->where('cart_product',$id);
            $query=$this->db->get('cart_detail');
            return count($query->result_array());
        }
        //delete array products
        public function delete($id=array())
        {
            $this->db->where_in('product_id',$id);
            $this->db->delete('product');
            return $this->db->affected_rows();
        }
        //publish or unpublish
        public function publish($status,$id=array())
        {
            $this->db->where_in('product_id',$id);
            if($status==0)
            {
                $this->db->update('product',array('product_publish'=>0));
            }
            else
            {
                $this->db->update('product',array('product_publish'=>1));
            }
            return $this->db->affected_rows();
        }
    }
?>

